package org.cocopapaya.springws.ext.xml;

import java.io.IOException;

import javax.xml.transform.Source;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.xml.transform.ResourceSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class CustomSchemaLoaderUtils {

	  /**
     * Load schema from the given resource.
     *
     * @param resource       the resource to load from
     * @param schemaLanguage the language of the schema. Can be <code>XMLConstants.W3C_XML_SCHEMA_NS_URI</code> or
     *                       <code>XMLConstants.RELAXNG_NS_URI</code>.
     * @throws IOException  if loading failed
     * @throws SAXException if loading failed
     * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI
     * @see javax.xml.XMLConstants#RELAXNG_NS_URI
     */
    public static Schema loadSchema(Resource resource, String schemaLanguage) throws IOException, SAXException {
        return loadSchema(new Resource[]{resource}, schemaLanguage);
    }

    /**
     * Load schema from the given resource.
     *
     * @param resources      the resources to load from
     * @param schemaLanguage the language of the schema. Can be <code>XMLConstants.W3C_XML_SCHEMA_NS_URI</code> or
     *                       <code>XMLConstants.RELAXNG_NS_URI</code>.
     * @throws IOException  if loading failed
     * @throws SAXException if loading failed
     * @see javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI
     * @see javax.xml.XMLConstants#RELAXNG_NS_URI
     */
    public static Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
        Assert.notEmpty(resources, "No resources given");
        Assert.hasLength(schemaLanguage, "No schema language provided");
        Source[] schemaSources = new Source[resources.length];
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        for (int i = 0; i < resources.length; i++) {
            Assert.notNull(resources[i], "Resource is null");
            Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
            schemaSources[i] = new ResourceSource(xmlReader, resources[i]);
        }
        SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
        schemaFactory.setResourceResolver(null);//set the LSresourceresolver which can load them from classpath
        return schemaFactory.newSchema(schemaSources);
    }

    /** Retrieves the URL from the given resource as System ID. Returns <code>null</code> if it cannot be opened. */
    public static String getSystemId(Resource resource) {
        try {
            return resource.getURL().toString();
        }
        catch (IOException e) {
            return null;
        }
    }

}
